package fr.ece.ing4.si.rockpaperscissors;


public class RockPaperScissors
{
	/**
	 * Method which gives the result of a rock paper scissors attempt
	 * @param p1
	 * @param p2
	 * @return result of the attempt for RPSEnum p1 (WIN, TIE or LOST)
	 */
	public Result play(RPSEnum p1, RPSEnum p2)
	{
		switch(p1)
		{
			default:
				System.out.println("Invalid RPSEnum p1");
				return Result.LOST;
				
			case ROCK:
				switch (p2)
				{
					default:
						System.out.println("Invalid RPSEnum p2");
						return Result.LOST;
						
					case ROCK:
						return Result.TIE;
						
					case PAPER:
						return Result.LOST;
						
					case SCISSORS:
						return Result.WIN;
				}
				
			case PAPER:
				switch (p2)
				{
					default:
						System.out.println("Invalid RPSEnum p2");
						return Result.LOST;
						
					case ROCK:
						return Result.WIN;
						
					case PAPER:
						return Result.TIE;
						
					case SCISSORS:
						return Result.LOST;
				}
				
			case SCISSORS:
				switch (p2)
				{
					default:
						System.out.println("Invalid RPSEnum p2");
						return Result.LOST;
						
					case ROCK:
						return Result.LOST;
						
					case PAPER:
						return Result.WIN;
						
					case SCISSORS:
						return Result.TIE;
				}
		}
	}
	
	
	/**
	 * Method which gives the result of a rock paper scissors party between two players according to their scores
	 * @param p1
	 * @param p2
	 * @return result of the party for player 1 (WIN, TIE or LOST)
	 */
	public Result play(Player p1, Player p2)
	{
		for (int i = 0; i < p1.getPlayerMovesListSize(); i++)
		{
			switch(play(p1.getNextMove(), p2.getNextMove()))
			{
				default:
					break;
					
				case WIN:
					p1.setScore(p1.getScore() + 1);
					break;
					
				case TIE:
					p1.setScore(p1.getScore() + 1);
					p2.setScore(p2.getScore() + 1);
					break;
					
				case LOST:
					p2.setScore(p2.getScore() + 1);
					break;
			}
		}
		
		if (p1.getScore() > p2.getScore())
		{
			return Result.WIN;
		}
		else if (p1.getScore() < p2.getScore())
		{
			return Result.LOST;
		}
		else
		{
			return Result.TIE;
		}
	}
}
